<template>
  <div>
    <el-dialog
      title="新建模型"
      :visible.sync="isNewModelDialog"
      width="500px"
      :before-close="handleClose"
      class="new-model-dialog"
    >
      <div class="text">
        <el-alert
          title="字典模型"
          type="success"
          description="指定一个文件名并选择一个要创建的模型对象"
          :closable="false"
        >
        </el-alert>
      </div>
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="80px"
        size="mini"
      >
        <el-form-item
          label="模型名称"
          prop="file"
        >
          <el-input v-model="form.file"></el-input>
        </el-form-item>
        <el-form-item
          label="模型对象"
          prop="model"
        >
          <el-select
            v-model="form.model"
            placeholder="请选择模型对象"
          >
            <el-option
              v-for="(item,index) in this.$store.state.objectNames"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="xml编码"
          prop="xml"
        >
          <el-select
            v-model="form.xml"
            placeholder="请选择XML编码"
            v-for="(item,index) in xmlList"
            :key="index"
          >
            <el-option
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

      </el-form>

      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          size="mini"
          @click="handleClose"
        >取 消</el-button>
        <el-button
          size="mini"
          type="primary"
          @click="submit"
          :loading="isLoading"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "newModelDialog",
  props: {
    isNewModelDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      rules: {
        file: { required: true, message: "请输入文件名称", trigger: "change" },
        model: { required: true, message: "请选择模型对象", trigger: "change" },
        xml: { required: true, message: "请输入xml编码", trigger: "change" },
      },
      xmlList: [{ lable: "UTF-8", value: "UTF-8" }],
      isLoading: false,
      form: {
        file: "",
        model: "",
        xml: "",
      },
    };
  },
  methods: {
    // 增加
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          console.log(this.form);
          this.addModel(this.form);
        }
      });
    },
    // 提交
    async addModel(data) {
      let params = {
        modelName: data.file,
        nodeType: data.model,
        parentId: null,
        reference: null,
      };
      this.isLoading = true;
      let res = await this.$http.post(
        "/dictionary/createModel?encoding=" + data.xml,
        params
      );
      this.isLoading = false;
      if (res.code == 200) {
        let $data = {
          id: res.data,
          name: data.file,
        };
        this.$router.push(`/?name=${$data.name}`);
        this.$emit("update", $data);
        this.handleClose();
      } else {
        this.$notify.error(res.msg);
      }
    },
    handleClose() {
      this.$emit("update:isNewModelDialog", false);
    },
  },
};
</script>

<style lang="scss">
.new-model-dialog {
  .el-select {
    width: 100%;
  }
  .text {
    margin-bottom: 20px;
  }
}
</style>